const express = require('express');
const static = require('express-static');
const path = require('path');
const _ = require('underscore');
const bodyParser = require('body-parser');

var Movie = require('./models/movie');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

mongoose.connect("mongodb://localhost:27017/test");

let port = 3000;

let app = express();

app.set('views', './views/pages');

app.set('view engine', 'jade');

app.use(static(path.join(__dirname, 'public')));

app.use(bodyParser.urlencoded({
    extended: true
}));

app.locals.moment = require('moment');

app.listen(port, '127.0.0.1');

console.log(`Service is running at ${port}`);

app.get('/', (req, res) => {
    Movie.fetch((err, movies) => {
        if (err) {
            return console.log(err);
        }
        console.log(movies);
        res.render('index', {
            title: "首页-列表",
            movies: movies
        });
    });
});

app.get('/detail/:id', (req, res) => {
    var id = req.params.id;
    console.log(id);
    Movie.findById(id, (err, movie) => {
        if (err) {
            return console.log(err);
        }
        res.render('detail', {
            title: '电影-详情',
            movie: movie
        });
    })
});

app.get('/admin', (req, res) => {
    Movie.fetch(function (err, movies) {
        if (err) {
            return console.log(err);
        }
        res.render('admin', {
            title: '后台-列表页',
            movies: movies
        });
    });
});

app.get('/admin/add', (req, res) => {
    res.render('add', {
        title: '电影录入',
        movie: {
            title: '',
            doctor: '',
            country: '',
            year: '',
            poster: '',
            flash: '',
            summary: '',
            language: ''
        }
    });
});

app.post('/admin/add', (req, res) => {
    var id = req.body.movie._id;
    var movieObj = req.body.movie;
    var _movie
    if (id !== 'undefined') {
        //修改数据
        Movie.findById(id, function (err, movie) {
            if (err) {
                return console.log(err);
            }
            _movie = new Movie(_.extend(movie, movieObj));
            _movie.save(function (err, movie) {
                if (err) {
                    return console.log(err);
                }
                res.redirect('/admin');
            })
        });
    } else {
        //添加数据
        _movie = new Movie({
            doctor: movieObj.doctor,
            title: movieObj.title,
            country: movieObj.country,
            language: movieObj.language,
            year: movieObj.year,
            poster: movieObj.poster,
            summary: movieObj.summary,
            flash: movieObj.flash,
        });

        _movie.save(function (err, movie) {
            if (err) {
                return console.log(err);
            }
            res.redirect('/admin');
        });
    }
});

app.get('/admin/update/:id', function (req, res) {
    var id = req.params.id;
    if (id) {
        Movie.findById(id, function (err, movie) {
            res.render('add', {
                title: '后台更新页',
                movie: movie
            })
        });
    }
});

app.delete('/admin/del', function (req, res) {
    var id = req.query.id;
    if (id) {
        Movie.remove({ _id: id }, function (err) {
            if (err) {
                return console.log(err);
            }
            res.json({ "status": 1, "info": "success" });
        });
    }
});